package com.wuyou.designPatterns.command;

import java.util.Iterator;
import java.util.Stack;

/**
 * 表示"由多条命令整合成的命令"
 * Macro 有"大量的"意思，一般表示"由多条命令整合的命令"
 * @Date 2024/10/18 15:54
 * @Author 无忧
 */
public class MacroCommand implements Command{

    private Stack commands = new Stack();

    @Override
    public void execute() {
        Iterator iterator = commands.iterator();
        while (iterator.hasNext()){
            ((Command)iterator.next()).execute();
        }
    }

    /**
     * 添加命令
     * @param cmd
     */
    public void append(Command cmd){
        if (cmd != this) {
            commands.push(cmd);
        }
    }

    /**
     * 删除最后一条命令
     */
    public void undo(){
        if(!commands.empty()){
            commands.pop();
        }
    }

    /**
     * 删除所有命令
     */
    public void clear(){
        commands.clear();
    }
}
